home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsI / Src / PolyScan / scantest.c < prev   
C/C++ Source or Header  |  1992-06-16  |  2KB  |  66 lines

  1. /*
  2.  * scantest.c: use poly_scan() for Gouraud shading and z-buffer demo.
  3.  * Given the screen space X, Y, and Z of N-gon on command line,
  4.  * print out all pixels during scan conversion.
  5.  * This code could easily be modified to actually read and write pixels.
  6.  *
  7.  * Paul Heckbert    Dec 1989
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include "poly.h"
  13.  
  14. #define XMAX 1280    /* hypothetical image width */
  15. #define YMAX 1024    /* hypothetical image height */
  16.  
  17. #define FRANDOM() ((rand()&32767)/32767.)   /* random number between 0 and 1 */
  18.  
  19. static void pixelproc();
  20.  
  21. main(ac, av)
  22. int ac;
  23. char **av;
  24. {
  25.     int i;
  26.     Poly p;
  27.     static Window win = {0, 0, XMAX-1, YMAX-1};    /* screen clipping window */
  28.  
  29.     if (ac<2 || ac != 2+3*(p.n = atoi(av[1]))) {
  30.     fprintf(stderr, "Usage: scantest N X1 Y1 Z1 X2 Y2 Z2 ... XN YN ZN\n");
  31.     exit(1);
  32.     }
  33.     for (i=0; i<p.n; i++) {
  34.     p.vert[i].sx = atof(av[2+3*i]);    /* set screen space x,y,z */
  35.     p.vert[i].sy = atof(av[3+3*i]);
  36.     p.vert[i].sz = atof(av[4+3*i]);
  37.     p.vert[i].r = FRANDOM();    /* random vertex colors, for kicks */
  38.     p.vert[i].g = FRANDOM();
  39.     p.vert[i].b = FRANDOM();
  40.     }
  41.     /* interpolate sx, sy, sz, r, g, and b in poly_scan */
  42.     p.mask = POLY_MASK(sx) | POLY_MASK(sy) | POLY_MASK(sz) |
  43.     POLY_MASK(r) | POLY_MASK(g) | POLY_MASK(b);
  44.  
  45.     poly_print("scan converting the polygon", &p);
  46.     
  47.     poly_scan(&p, &win, pixelproc);    /* scan convert! */
  48. }
  49.  
  50. static void pixelproc(x, y, point)    /* called at each pixel by poly_scan */
  51. int x, y;
  52. Poly_vert *point;
  53. {
  54.     printf("pixel (%d,%d) screenz=%g rgb=(%g,%g,%g)\n",
  55.     x, y, point->sz, point->r, point->g, point->b);
  56.  
  57.     /*
  58.      * in real graphics program you could read and write pixels, e.g.:
  59.      *
  60.      *    if (point->sz < zbuffer_read(x, y)) {
  61.      *        image_write_rgb(x, y, point->r, point->g, point->b);
  62.      *        zbuffer_write(x, y, point->sz);
  63.      *  }
  64.      */
  65. }
  66.